package sh.wnxy.wn38.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sun.deploy.util.BlackList;
import io.swagger.annotations.ApiModel;
import org.apache.ibatis.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import sh.wnxy.wn38.entity.Cart;

import sh.wnxy.wn38.entity.Ticket;
import sh.wnxy.wn38.entity.User;
import sh.wnxy.wn38.service.ICartService;

import sh.wnxy.wn38.utils.Result;
import sh.wnxy.wn38.utils.vo.fakeCart;


import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wn38
 * @since 2021-07-13
 */
@ApiModel("购物车")
@CrossOrigin
@RestController
@RequestMapping("/cart")
public class CartController {



    private ICartService iCartService;
    @Autowired
    public void setiCartService(ICartService iCartService) {
        this.iCartService=iCartService;
    }
    @PostMapping("/add")
    public Result add(@RequestBody Cart cart) {
        QueryWrapper<Cart> ac = new QueryWrapper();
        if (!StringUtils.isEmpty(cart.getCartUserid())) {
            ac.eq("cart_userid", cart.getCartUserid());
        }
        if (!cart.getCartTicketid().equals(0)) {
            ac.eq("cart_ticketid", cart.getCartTicketid());
        }
        Cart one = iCartService.getOne(ac);
        if (one != null) {
            UpdateWrapper<Cart> wrapper = new UpdateWrapper<Cart>();
            wrapper.setSql("cart_num = cart_num+ " + cart.getCartNum());
            wrapper.eq("cart_ticketid", cart.getCartTicketid());
            wrapper.eq("cart_userid", cart.getCartUserid());
            iCartService.update(wrapper);
            return Result.ok().msg("增加购物车成功");
        } else {
            if (iCartService.save(cart))
                return Result.ok();
            else
                return Result.error();
        }
    }
    @PostMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean b = iCartService.removeById(id);
        if(b)
        return Result.ok();
        else
            return Result.error();
    }


    //项目。图片预览，票种，价格，数量，
    @PostMapping("selectAll/{id}")
    public Result selectAll(@PathVariable("id") String id){
        List<Cart> list = iCartService.selectall(id);
        System.out.println(list.size());
        List<fakeCart> cartList = new ArrayList<>();
        for (Cart c:list) {
            fakeCart cart = new fakeCart();
            cart.setNumber(c.getCartNum());
            cart.setCartId(c.getCartId());



            Ticket ticket=iCartService.selectTicekt(c.getCartTicketid());



            cart.setKind(ticket.getTicketVersion());
            cart.setPrice(ticket.getTicketPrice());
            cart.setDateTime(ticket.getTicketDate());
            cart.setTicketStock(ticket.getTicketStock());
            String spotName = iCartService.getSpotName(ticket.getTicketSpotid());
            cart.setSpotName(spotName);
            String path=iCartService.selectPath(ticket.getTicketSpotid());
            cart.setPath(path);
            cart.setUserId(c.getCartUserid());
            cartList.add(cart);
        }
        return Result.ok().data("cartList",cartList);
    }
    @PostMapping("update/{index}/{number}")
    public Result update(@PathVariable("index") Integer index,@PathVariable("number") Integer number){
        if(iCartService.updateNumber(index,number))
            return Result.ok();
        else
            return Result.error();

    }
    @PostMapping("changeTicket/{index}/{date}")
    public Result update2(@PathVariable("index") Integer index,@PathVariable("date")@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate date){
        System.out.println(date);
        Ticket ticket = iCartService.changeTicket(index, date);
        System.out.println(1111111);
        return Result.error().data("ticket",ticket);

    }



}

